package com.card.match;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.card.common.CardContants;
import com.card.common.MessageComm;
import com.card.user.User;
import com.card.user.UserManager;
import com.card.user.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xiaoao.net.json.message.JsonMessage;
import com.xiaoao.net.utils.StringUtil;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author sky  
 * @date 2016年11月3日 上午9:32:01 
 * @version V1.0   
 */
public class MatchManager {
	public static final String T_Table = "matchdata";

	private static MatchManager instance;
    public static MatchManager getInstance() {
    	if(instance == null) {
    		instance = new MatchManager();
    		instance.initSetting();
    	}
        return instance;
    }
    
    private List<MatchSetting> settingList;
    private Map<Integer,MatchSetting> settingMap = Maps.newConcurrentMap();
    private Map<Integer,Match> typeMatchMap = Maps.newConcurrentMap();
    private Map<String,Match> runMatchMap = Maps.newConcurrentMap(); 
    
    private void initSetting() {
    	settingList = MatchSetting.getMatchSettings();
    	if(settingList.isEmpty()) {
    		MatchSetting ms1 = new MatchSetting(CardContants.GAMETYPE_SH, "16人快速赛", 16, 2);
        	MatchSetting ms2 = new MatchSetting(CardContants.GAMETYPE_SH, "16人快速赛", 16, 1);
        	MatchSetting ms3 = new MatchSetting(CardContants.GAMETYPE_Ddz, "9人快速赛", 9, 2);
        	MatchSetting ms4 = new MatchSetting(CardContants.GAMETYPE_Ddz, "9人快速赛", 9, 1);
        	MatchSetting ms5 = new MatchSetting(CardContants.GAMETYPE_TGG, "9人快速赛", 9, 1);
        	MatchSetting ms6 = new MatchSetting(CardContants.GAMETYPE_Nn, "9人快速赛", 9, 1);
        	MatchSetting ms7 = new MatchSetting(CardContants.GAMETYPE_Pdk, "9人快速赛", 9, 2);
        	MatchSetting ms8 = new MatchSetting(CardContants.GAMETYPE_Pdk, "9人快速赛", 9, 1);
        	settingList = MatchSetting.getMatchSettings();
    	}
    	Collections.sort(settingList);
    	for (MatchSetting matchSetting : settingList) {
    		settingMap.put(matchSetting.getId(), matchSetting);
    		addMatch(matchSetting.getId());
		}
    }
    
    public Match getMatch(int type) {
    	return typeMatchMap.get(type);
    }
    
    public Match getMatchById(String id) {
    	if(StringUtil.NullOrEmpty(id)) {
    		return null;
    	}
    	Match match = getRunMatch(id);
    	if(match == null) {
    		for (Match temp : typeMatchMap.values()) {
				if(StringUtil.equals(id, temp.getId())) {
					return temp;
				}
			}
    		return null;
    	}
    	return match;
    }
    
    public Match addMatch(int type) {
    	MatchSetting setting = getMatchSetting(type);
    	if(!setting.isOpen()) {
    		return null;
    	}
    	Match match = new Match(setting);
		typeMatchMap.put(type, match);
		runMatchMap.put(match.getId(), match);
		match.startTime();
		MatchManager.getInstance().broadMatchUpdate();
		return  match;
    }
    
//    public Match joinMatch(User user,int id) {
//    	Match match = getMatch(id);
//    	if(!match.isContains(user.getPrimaryKey())) {
//    		match.addUser(user);
//    		return match;
//    	}
//    	return null;
//    }
    
    public Match getRunMatch(String id) {
    	return runMatchMap.get(id);
    }
    public void deleteRunMatch(String id) {
    	runMatchMap.remove(id);
    }
    
    public void fillMsg(JsonMessage message) {
		List<ShowMatch> showList = Lists.newArrayList();
		for (MatchSetting setting : settingList) {
			Match match = typeMatchMap.get(setting.getId());
			if(match != null) {
				showList.add(new ShowMatch(match));
			}
		}
    	message.addProperty("matchList", showList);
    }
    public void broadMatchUpdate() {
		JsonMessage message2 = JsonMessage.createMessage(MessageComm.S2C_Match_List);
		MatchManager.getInstance().fillMsg(message2);
		UserManager.getInstance().broadMessageOutRoom(message2);
	}
    
    public List<MatchSetting> getSettingList() {
		return settingList;
	}
    public MatchSetting getMatchSetting(int id) {
    	return settingMap.get(id);
    }
    
    public void closeMatch(int id) {
    	Match match = typeMatchMap.get(id);
    	if(match != null && match.getUserSize()==0) {
    		typeMatchMap.remove(id);
    	}
    }
    
    public void test() {
    	User u1 = UserUtils.getUserByImei("1");
		User u2 = UserUtils.getUserByImei("2");
		
		Match match = MatchManager.getInstance().getMatch(1);
		match.addUser(u1);
		match.addUser(u2);
    }
    
}
